package com.inputabc.ct.v1.ui.inputbox.component;

import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.*;

public class InputBoxScrollPane extends JScrollPane {
    private InputBoxTextArea inputBoxTextArea;
    
    // 定义滚动条配色
    private static final Color THUMB_COLOR = new Color(180, 180, 180);
    private static final Color THUMB_HOVER_COLOR = new Color(140, 140, 140);
    private static final Color TRACK_COLOR = new Color(245, 245, 245);
    
    {
        setName(getClass().getName());
        inputBoxTextArea = new InputBoxTextArea();
    }
    
    public InputBoxScrollPane() {
        setViewportView(inputBoxTextArea);
        setAutoscrolls(true);
        
        // 设置垂直滚动条策略
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        
        // 启用平滑滚动
        getVerticalScrollBar().setUnitIncrement(16);
        
        // 设置滚动条宽度
        getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
        
        // 自定义滚动条UI
        customizeScrollBarUI();
        
        // 设置透明背景，以适应圆角边框
        setOpaque(false);
        getViewport().setOpaque(false);
        setBorder(BorderFactory.createEmptyBorder());
    }
    
    /**
     * 自定义滚动条UI样式
     */
    private void customizeScrollBarUI() {
        // 设置垂直滚动条UI
        getVerticalScrollBar().setUI(new ModernScrollBarUI());
        getVerticalScrollBar().setOpaque(false);
        
        // 移除按钮
        getVerticalScrollBar().setFocusable(false);
    }
    
    /**
     * 现代风格滚动条UI
     */
    private class ModernScrollBarUI extends BasicScrollBarUI {
        private final int THUMB_SIZE = 8;
        
        @Override
        protected void configureScrollBarColors() {
            this.thumbColor = THUMB_COLOR;
            this.thumbDarkShadowColor = THUMB_COLOR;
            this.thumbHighlightColor = THUMB_COLOR;
            this.thumbLightShadowColor = THUMB_COLOR;
            this.trackColor = TRACK_COLOR;
            this.trackHighlightColor = TRACK_COLOR;
        }
        
        @Override
        protected JButton createDecreaseButton(int orientation) {
            return createEmptyButton();
        }
        
        @Override
        protected JButton createIncreaseButton(int orientation) {
            return createEmptyButton();
        }
        
        private JButton createEmptyButton() {
            JButton button = new JButton();
            button.setPreferredSize(new Dimension(0, 0));
            button.setMinimumSize(new Dimension(0, 0));
            button.setMaximumSize(new Dimension(0, 0));
            return button;
        }
        
        @Override
        protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
            if (thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
                return;
            }
            
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            // 获取鼠标悬停状态
            boolean isRollover = isThumbRollover();
            
            // 根据鼠标悬停状态选择颜色
            g2.setColor(isRollover ? THUMB_HOVER_COLOR : THUMB_COLOR);
            
            // 居中绘制滚动条滑块
            int x = thumbBounds.x + (thumbBounds.width - THUMB_SIZE) / 2;
            int y = thumbBounds.y;
            int width = THUMB_SIZE;
            int height = thumbBounds.height;
            
            // 绘制圆角矩形
            g2.fillRoundRect(x, y, width, height, width, width);
            g2.dispose();
        }
        
        @Override
        protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(TRACK_COLOR);
            g2.fillRoundRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height, 10, 10);
            g2.dispose();
        }
    }
    
    /**
     * 重写绘制方法，支持透明背景和抗锯齿
     */
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        super.paintComponent(g2);
        g2.dispose();
    }

    public InputBoxTextArea getInputBoxTextArea() {
        return inputBoxTextArea;
    }

    public void setInputBoxTextArea(InputBoxTextArea inputBoxTextArea) {
        this.inputBoxTextArea = inputBoxTextArea;
    }
}
